package com.swkl.yuan.agvtask.task;

import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
import com.swkl.yuan.agvtask.entity.AgvTask;
import com.swkl.yuan.agvtask.entity.AllConfig;
import com.swkl.yuan.agvtask.service.AgvConfigService;
import com.swkl.yuan.agvtask.service.AgvTaskService;
import com.swkl.yuan.agvtask.service.SendRequestTask;
import com.swkl.yuan.agvtask.service.SystemConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * Project: AGVTask  package:  com.swkl.yuan.agvtask.task  -  MainTask
 * Powered by zhiyuan On 2024-06-18 16:44:27
 *
 * @author le9971@126.com
 * @version 1.0
 */
@Service
@Slf4j
public class MainTask {

    @Resource
    SystemConfigService systemConfigService;
    @Resource
    AgvTaskService agvTaskService;
    @Resource
    AgvConfigService agvConfigService;
    @Resource
    SendRequestTask sendRequestTask;
    S7PLC connector ;
    boolean flag = false;
    List<AgvTask> allAgvTask;
    AllConfig allConfig = new AllConfig();
    public void init(){
        //初始话配置信息
        allConfig.intConfig(agvConfigService.getAgvConfig(),systemConfigService.getSystemConfig());
        allAgvTask = agvTaskService.getAllAgvTask();
        connector = connectorPlc(allConfig);
        flag = true;
    }


    @Scheduled(fixedDelay = 1500)
    public void startRun(){
        if (flag){
            for (AgvTask agvTask : allAgvTask){
                if(agvTask.getState().equals(1)){
                    readDistribution(agvTask);
                }else{
                    return;
                }
            }
        }
        //log.info("这是第{}此日志输出", Optional.of(i++));

    }
    @Scheduled(fixedDelay = 2000)
    public   void heartbeatService(){
        if (flag){
            try {
                SendToPlc.heartbeat(connector,allConfig.heartbeatOffset);
            }catch (Exception e){
                log.error("心跳发生异常，等待下次连接。异常信息={}",e.getMessage());
            }
        }
    }


    private void readDistribution(AgvTask agvTask){
        boolean aBoolean;
        try {
            Thread.sleep(50);
            short i = S7PLCUtil.readInt16(connector, agvTask.getPlcMonitorOffset());
            //log.info("偏移量为 = {} 读取数值为 = {} ",agvTask.getPlcMonitorOffset(),i);
            aBoolean = toBoolean(i);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        if (aBoolean){
            log.info("{}-接收移动，开始发送请求开始位置{}——结束位置{}",agvTask.getTaskName(),agvTask.getStartLocation(),agvTask.getEndLocation());
            sendRequestTask.sendRequest(agvTask,allConfig,connector);
        }
    }

    private S7PLC connectorPlc(AllConfig allConfig){
        return new S7PLC(getPlcType(allConfig.plcType), allConfig.ip);
    }


    private  EPlcType getPlcType(String type){
        switch (type){
            case "S200":
                return EPlcType.S200;
            case "S200_SMART":
                return EPlcType.S200_SMART;
            case "S300":
                return EPlcType.S300;
            case "S400":
                return EPlcType.S400;
            case "S1200":
                return EPlcType.S1200;
            case "S1500":
                return EPlcType.S1500;
            case "828D":
                return EPlcType.SINUMERIK_828D;
            default:
                flag =false;
                log.error("类型选择一场请重新选择");
                return EPlcType.SINUMERIK_828D;
        }
    }


    private boolean toBoolean(int i){
        return i == 1;
    }
}

